package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    /**
     * 查询所有护理等级
     *
     * @return
     */
    @Override
    public List<NursingLevelVo> listAll() {

        return nursingLevelMapper.listAll();
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevelDto
     */
    @Override
    public void levelAdd(NursingLevelDto nursingLevelDto) {
        NursingLevel level = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        level.setCreateTime(LocalDateTime.now());
        level.setUpdateTime(LocalDateTime.now());
        nursingLevelMapper.insertLevel(level);
    }

    /**
     * 护理等级分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingLevelVo> levelSearchPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        //使用PageHelper进行分页
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper方法查询
        Page<NursingLevelVo> levelVos = nursingLevelMapper.selectByPage(name, status);
        return PageResponse.of(levelVos,NursingLevelVo.class);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public NursingLevelVo getById(Integer id) {
        NursingLevelVo vo = nursingLevelMapper.selectById(id);
        return vo;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevelDto
     */
    @Override
    public void update(NursingLevelDto nursingLevelDto) {
        NursingLevelVo vo = BeanUtil.toBean(nursingLevelDto, NursingLevelVo.class);
        nursingLevelMapper.update(vo);
    }

    /**
     * 删除护理等级
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        nursingLevelMapper.delete(id);
    }

    /**
     * 分页查询
     * @param id
     * @param status
     */
    @Override
    public void setStatus(Integer id, Integer status) {
        nursingLevelMapper.setStatus(id, status);
    }
}
